//
//  RHMqttFuncListController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/14.
//

#import "RHMqttFuncListController.h"
#import "RHDevRelationController.h"
#import "RHMQReportParamController.h"
#import "RHMQEventReportController.h"
#import "RHMQEndPointDataController.h"
#import "RHMQUpgradeListController.h"
#import "RHMQDevInfoController.h"
#import "RHMQCommandListController.h"
#import "RHMQTTManage.h"
#import "RHMQDevModel.h"

@interface RHMqttFuncListController ()<UITableViewDelegate ,UITableViewDataSource>

@property (nonatomic ,strong) UITableView *tableView;

@property (nonatomic ,copy) NSArray *titlesArray;


@end

@implementation RHMqttFuncListController


- (void)viewDidLoad {
    [super viewDidLoad];
    _titlesArray = @[@"上报设备端点关系",@"上报参数信息",@"上传故障/告警信息",@"数据点上报",@"接收指令",@"固件升级",@"设备统一下发入口"];
    [self.navigationItem setTitle:@"模拟功能列表"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(noticUpdateInfo:) name:DeviecStateChangeNotification object:nil];
}

- (void)dealloc{
    [[RHMQTTManage shareMQTTManage] disConnectMqtt:^(BOOL isSuccess) {
            
    }];
}

- (void)setupUI{
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    [_tableView setBackgroundColor:CFTabViewBgColor];
    [_tableView setDelegate:self];
    [_tableView setDataSource:self];
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [self.view addSubview:_tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

#pragma mark - NSNotification
- (void)noticUpdateInfo:(NSNotification *)notification{
    NSLog(@"-----");
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate ,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section == 1 ? self.titlesArray.count:1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
    QMUILabel *lab = [[QMUILabel alloc] init];
    [lab setTextAlignment:NSTextAlignmentLeft];
    [lab setFont:[UIFont systemFontOfSize:13]];
    [lab setTextColor:kCOLOR_HEX(@"666666")];
    [lab setText:section == 0 ? @"设备":(section == 1?@"功能":@"")];
    [headView addSubview:lab];
    
    [lab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.bottom.mas_offset(0);
        make.height.mas_equalTo(30);
        make.right.mas_offset(-15);
    }];
    
    return headView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    
    if(indexPath.section == 1){
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        [cell.textLabel setText:self.titlesArray[indexPath.row]];
        [cell.textLabel setTextColor:kCOLOR_HEX(@"666666")];
        [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
        [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        [cell setAccessoryView:nil];
    }else if (indexPath.section == 2){
        QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:[RHMQTTManage shareMQTTManage].devModel.isOnline? @"断开连接":@"重新连接" forState:UIControlStateNormal];
        [btn setTitleColor:[RHMQTTManage shareMQTTManage].devModel.isOnline?[[UIColor redColor] colorWithAlphaComponent:0.7]:CFMainColor forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
        [btn setFrame:CGRectMake(0, 0, SCREEN_WIDTH-30, 40)];
        [btn setEnabled:NO];
        [cell setAccessoryView:btn];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }else{
        [cell.textLabel setFont:[UIFont systemFontOfSize:14]];
        [cell.textLabel setTextColor:kCOLOR_HEX(@"666666")];
        [cell.textLabel setText:[RHMQTTManage shareMQTTManage].devModel.devId];
        
        QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:[RHMQTTManage shareMQTTManage].devModel.isOnline?@"在线":@"离线" forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"arrow"] forState:UIControlStateNormal];
        [btn setTitleColor:[RHMQTTManage shareMQTTManage].devModel.isOnline?CFMainColor:kCOLOR_HEX(@"999999") forState:UIControlStateNormal];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:13]];
        [btn setImagePosition:QMUIButtonImagePositionRight];
        [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
        [btn setFrame:CGRectMake(0, 0, 100, 40)];
        [btn setEnabled:NO];
        [cell setAccessoryView:btn];
        [cell setAccessoryType:UITableViewCellAccessoryNone];
    }

    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section == 0){//设备状态
        RHMQDevInfoController *vc = [[RHMQDevInfoController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
        return;
    }
    
    if(indexPath.section == 2){//断开设备
        
        if([RHMQTTManage shareMQTTManage].devModel.isOnline){//断开
            [[RHMQTTManage shareMQTTManage] disConnectMqtt:^(BOOL isSuccess) {
                
            }];
        }else{//重连
            [[RHMQTTManage shareMQTTManage] connectMqtt:^(BOOL isSuccess) {
                
            }];
        }
        return;
    }
    
    
    if(indexPath.row == 0){//上报端点
        RHDevRelationController *vc = [[RHDevRelationController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 1){//上报参数
        RHMQReportParamController *vc = [[RHMQReportParamController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 2){//上传故障/告警
        RHMQEventReportController *vc = [[RHMQEventReportController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 3){//数据点上报
        RHMQEndPointDataController *vc = [[RHMQEndPointDataController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 4){//接收指令
        RHMQCommandListController *vc = [[RHMQCommandListController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 5){//固件升级
        RHMQUpgradeListController *vc = [[RHMQUpgradeListController alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
    if(indexPath.row == 6){//设备统一下发入口,模拟修改设备参数
        NSDictionary *para = @{
            @"protocolHead": @{
              @"cmdId": @"35f54a1f-5f01-4b8a-88e7-5490b5140e0d",
              @"deviceId": @"ZR2991ME1001046",
              @"deviceType": @"DCDCIN",
              @"messageType": @"M_Control",
              @"namespace": @"1",
              @"payloadVersion": @(1),
              @"timestamp": @(15648432)
            },
            @"protocolBody": @{
              @"commandParamList": @[
                @{
                  @"dataType": @"int",
                  @"paramName": @"Pc_key1",
                  @"value": @"20"
                }
              ],
              @"pointData": @{
                @"data": @"",
                @"deviceId": @"",
                @"endpointCode": @"",
                @"sn": @"M_0000001"
              }
             
            }
            
          };
        [QMUITips showLoadingInView:self.view];
        [BaseModel startWithCompletionAPIKey:H_controlDevice argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
             
            if(resultModel.code == 200){
                [QMUITips showSucceed:@"success"];
            }
            
        } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
            
        }];
    }
}

@end
